package com.zlzxm.shoesmarket.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.UserManager;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.viewmodel.VMMine;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityUserInfo;

/**
 * CREATE BY zlzxm ON 4/7/21
 */
public class FMNavigationMine extends BaseFragment implements View.OnClickListener{

    private static final String TAG = "FMNavigationMine";

    private VMMine vmMine;

    private AppBarLayout appBarLayout;

    private TextView tvUserName,tvPhone,tvToolUserName,tvVipInfo,tvVipRecharge;

    private ImageView ivFace;

    private int appbarScrollRang = 0;

    private RelativeLayout rlMine;

    private LinearLayout llRepositoryManager;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_mine,container,false);
    }

    @Override
    protected void initView(@NonNull View view, @NonNull Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);

        appbarScrollRang = ZViewHelp.dpTopx(getContext(),130 - 44);

        appBarLayout = ZViewHelp.findById(view,R.id.appBarLayout);
        tvUserName = ZViewHelp.findById(view,R.id.tvUserName);
        tvPhone = ZViewHelp.findById(view,R.id.tvPhone);
        tvToolUserName = ZViewHelp.findById(view,R.id.tvToolUserName);
        tvVipInfo = ZViewHelp.findById(view,R.id.tvVipInfo);
        tvVipRecharge = ZViewHelp.setOnClickListener(view,R.id.tvVipRecharge,this);
        ivFace = ZViewHelp.findById(view,R.id.ivFace);
        rlMine = ZViewHelp.setOnClickListener(view,R.id.rlMine,this);
        llRepositoryManager = ZViewHelp.setOnClickListener(view,R.id.llRepositoryManager,this);
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        vmMine = new ViewModelProvider(this).get(VMMine.class);

        vmMine.getLiveUser().observe(this, new Observer<EntityUserInfo>() {
            @Override
            public void onChanged(EntityUserInfo entityUserInfo) {

                updateUser(entityUserInfo.getName(),entityUserInfo.getPhone(),entityUserInfo.getFaceUlr()
                        ,entityUserInfo.getVipInfo());
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                appBarChange(verticalOffset);

            }
        });

        vmMine.requestUser();
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.rlMine){

            if(UserManager.getInstance().isLogin()) {

                ActivityIndex.startACAccount(getContext());
            }else {


                ActivityIndex.startACLogin(getContext());
            }

        }else if(v.getId() ==R.id. tvVipRecharge){

            ActivityIndex.startACVipInfo(getContext());

        }else if(v.getId() == R.id.llRepositoryManager){

            ActivityIndex.startACRepositoryManager(getContext());
        }

    }


    //<editor-fold defaultState="collapsed" desc="私有方法">



    private void appBarChange(int translation){


        if(translation == 0){

            tvToolUserName.setVisibility(View.GONE);

        }else {

            tvToolUserName.setVisibility(View.VISIBLE);
        }

    }

    private void updateUser(String user,String phone,String url,String vipInfo){

        tvUserName.setText(user);
        tvPhone.setText(phone);
        tvToolUserName.setText(user);
        tvVipInfo.setText(vipInfo);

        Glide.with(this)
                .load(url)
                .circleCrop()
                .into(ivFace);


    }

    // </editor-fold>

}
